package org.zht.dormpro.pub.exception;

import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.zht.dormpro.pub.util.WebUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

public class GlobleExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        ModelAndView modelAndView = new ModelAndView();
        //如果是认证失败的异常,返回错误信息
        if(e instanceof AuthorizationException && WebUtil.isAjax(httpServletRequest)){
            FastJsonJsonView view = new FastJsonJsonView();
            Map<String,Object> map = new HashMap<>();
            map.put("status",1001);
            map.put("desc","暂无权限!");
            view.setAttributesMap(map);
            modelAndView.setView(view);
            return modelAndView;
        }
        return null;
    }
}
